package cn.edu.mju.dilidili.modules.dynamic.serviceImpl;

import cn.edu.mju.dilidili.common.utils.AuthUtil;
import cn.edu.mju.dilidili.common.utils.Result;
import cn.edu.mju.dilidili.modules.dynamic.dto.CreateRequestDto;
import cn.edu.mju.dilidili.modules.dynamic.dto.CreateResponseDto;
import cn.edu.mju.dilidili.modules.dynamic.entity.Dynamic;
import cn.edu.mju.dilidili.modules.dynamic.mapper.DynamicMapper;
import cn.edu.mju.dilidili.modules.dynamic.service.DynamicService;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 动态信息表 服务实现类
 * </p>
 *
 * @author l
 * @since 2022-09-24
 */
@Service
public class DynamicServiceImpl extends ServiceImpl<DynamicMapper, Dynamic> implements DynamicService {

    @Override
    public Result<Page<Dynamic>> getPage(Integer page) {
        return new Result<>(this.page(new Page<>(page, 10)));
    }

    @Override
    public Result<CreateResponseDto> create(CreateRequestDto createRequestDto) {
        Dynamic dynamic = BeanUtil.copyProperties(createRequestDto, Dynamic.class);
        dynamic.setCreationTime(new Date());
        dynamic.setActionNumber(0);
        dynamic.setForwardNumber(0);
        dynamic.setHateNumber(0);
        dynamic.setReplyNumber(0);
        dynamic.setViewNumber(0);
        Map<String, Object> userInfo = AuthUtil.getUserInfo();
        dynamic.setAuthorId((Integer) userInfo.get("id"));
        dynamic.setAuthorName((String) userInfo.get("nickname"));
        this.baseMapper.insert(dynamic);
        return new Result<>(new CreateResponseDto(dynamic.getId()));
    }

    @Override
    public Result<Dynamic> getById(Integer id) {
        Dynamic dynamic = super.getById(id);
        dynamic.setViewNumber(dynamic.getViewNumber() + 1);
        this.updateById(dynamic);
        return new Result<>(dynamic);
    }
}
